package com.corgi.dubbo.provider.config;

import com.corgi.dubbo.api.service.UserService;
import org.apache.dubbo.config.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @title: DubboConfig
 * @description:
 * @author: dengmiao
 * @create: 2019-09-25 13:20
 **/
@Configuration
public class DubboConfig {

	/**
	 * 相当于替换dubbo:application标签
	 * <dubbo:application name="user-service-provider"></dubbo:application>
	 * @return
	 */
	@Bean
	public ApplicationConfig applicationConfig() {
		ApplicationConfig application = new ApplicationConfig();
		application.setName("user-service-provider");
		return application;
	}

	/**
	 * 注册中心
	 * <dubbo:registry address="zookeeper://127.0.0.1:2181" />
	 * @return
	 */
	@Bean
	public RegistryConfig registry() {
		RegistryConfig registry = new RegistryConfig();
		registry.setProtocol("zookeeper");
		registry.setAddress("127.0.0.1:2181");
		return registry;
	}

	/**
	 * 通信规则
	 * <dubbo:protocol name="dubbo" port="20880" />
	 * @return
	 */
	@Bean
	public ProtocolConfig protocol() {
		ProtocolConfig protocol = new ProtocolConfig();
		protocol.setName("dubbo");
		protocol.setPort(20880);
		return protocol;
	}

  /**
   * <dubbo:service interface="com.corgi.dubbo.api.service.UserService" ref="userServiceImpl"
   * version="1.0.0"> <dubbo:method name="findList" timeout="5000"/> </dubbo:service>
   *
   * @param userServiceImpl
   * @return
   */
  @Bean
  public ServiceConfig<UserService> userService(UserService userServiceImpl) {
		ServiceConfig<UserService> userService = new ServiceConfig<>();
		userService.setInterface(UserService.class);
		userService.setRef(userServiceImpl);
		userService.setVersion("1.0.0");

		MethodConfig method = new MethodConfig();
		method.setName("findList");
		method.setTimeout(5000);

		userService.setMethods(Arrays.asList(method));

		return userService;
	}
}
